<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title>lvs&#43;keepalive配置Jenkins2高可用 - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="lvs&#43;keepalive配置Jenkins2高可用">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content="lvs&#43;keepalive配置Jenkins2高可用 - 天地维杰网" />
    <meta name="description" content="lvs&#43;keepalived部署jenkins2高可用，包括部署步骤，参考文档等等" />
     
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/lvs_keepalive_jenkins2_ha/" itemprop="url">
        lvs&#43;keepalive配置Jenkins2高可用
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="2020-02-22">
    2020-02-22
</time>
</span> 
      

  <span class="post-category" >
  &nbsp; | &nbsp;
  <span class="post-meta-item-icon">
    <i class="fa fa-folder-o"></i>
  </span>
  <span class="post-meta-item-text">分类：</span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/linux" itemprop="url" rel="index">
        <span itemprop="name">linux</span>
      </a>
      &nbsp; 
    </span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/cicd" itemprop="url" rel="index">
        <span itemprop="name">cicd</span>
      </a>
      &nbsp; 
    </span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/daily" itemprop="url" rel="index">
        <span itemprop="name">daily</span>
      </a>
      &nbsp; 
    </span>
  
</span>


      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">5569 字 ~12分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h2 id="部署清单">部署清单</h2>

<table>
<thead>
<tr>
<th>node</th>
<th>trunk</th>
<th>neibu</th>
<th>release</th>
</tr>
</thead>

<tbody>
<tr>
<td>master</td>
<td>10.155.10.171:8093</td>
<td>10.155.10.172:8091</td>
<td>10.155.10.173:8092</td>
</tr>

<tr>
<td>slave</td>
<td>10.155.10.174:8093</td>
<td>10.155.10.174:8091</td>
<td>10.155.10.174:8092</td>
</tr>
</tbody>
</table>

<h2 id="部署步骤">部署步骤</h2>

<h3 id="安装和启动ipvsadm">安装和启动ipvsadm</h3>

<p>安装lvs</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">yum -y install ipvsadm</code></pre></div>
<p>启动lvs</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">systemctl start ipvsadm</code></pre></div>
<p>如果出现ipvsadm服务报错</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">/bin/bash: /etc/sysconfig/ipvsadm: No such file or directory</code></pre></div>
<p>则执行命令</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">ipvsadm --save &gt; /etc/sysconfig/ipvsadm</code></pre></div>
<p>再查看 ipvsadm的启动状态 <code>systemctl status ipvsadm</code></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic">#systemctl status ipvsadm</span>
● ipvsadm.service - Initialise the Linux Virtual Server
    Loaded: loaded <span style="color:#ce5c00;font-weight:bold">(</span>/usr/lib/systemd/system/ipvsadm.service<span style="color:#000;font-weight:bold">;</span> disabled<span style="color:#000;font-weight:bold">;</span> vendor preset: disabled<span style="color:#ce5c00;font-weight:bold">)</span>
    Active: active <span style="color:#ce5c00;font-weight:bold">(</span>exited<span style="color:#ce5c00;font-weight:bold">)</span> since 日 2020-02-09 12:39:02 CST<span style="color:#000;font-weight:bold">;</span> 4min 49s ago
    Process: <span style="color:#0000cf;font-weight:bold">46374</span> <span style="color:#000">ExecStart</span><span style="color:#ce5c00;font-weight:bold">=</span>/bin/bash -c <span style="color:#204a87">exec</span> /sbin/ipvsadm-restore &lt; /etc/sysconfig/ipvsadm <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">code</span><span style="color:#ce5c00;font-weight:bold">=</span>exited, <span style="color:#000">status</span><span style="color:#ce5c00;font-weight:bold">=</span>0/SUCCESS<span style="color:#ce5c00;font-weight:bold">)</span>
    Main PID: <span style="color:#0000cf;font-weight:bold">46374</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">code</span><span style="color:#ce5c00;font-weight:bold">=</span>exited, <span style="color:#000">status</span><span style="color:#ce5c00;font-weight:bold">=</span>0/SUCCESS<span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#8f5902;font-style:italic"># 如上说明服务正常启动</span></code></pre></div>
<h3 id="安装和启动keepalived">安装和启动keepalived</h3>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#8f5902;font-style:italic">#安装依赖应用（没有使用源码包方式安装，因为配置超麻烦）</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@localhost ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@localhost ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@localhost ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># yum install -y keepalived</span>

<span style="color:#8f5902;font-style:italic">#yum安装的 keepalived配置文件在 /etc/keepalived 目录中</span>

<span style="color:#ce5c00;font-weight:bold">[</span>root@localhost ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># systemctl start keepalived   //启动keepalived</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@localhost ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># systemctl restart keepalived  //重新启动keepalived</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@localhost ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># systemctl status keepalived   //查看keepalived状态</span>

<span style="color:#8f5902;font-style:italic">#如果主备服务器间网络不通，则需要关闭防火墙</span>
systemctl stop firewalld
systemctl disable firewalld
setenforce <span style="color:#0000cf;font-weight:bold">0</span>

<span style="color:#8f5902;font-style:italic">#启动keepalived以后，查看是否出现虚拟ip，主备上都要执行查看，正常应该只有主出现虚拟ip，备不出现，如果主备都出现了，说明脑裂了。</span>
1、主备之间是否通信正常，若不正常是否有防火墙阻挡
2、主备对应的keepalived.conf配置是否有误，virtual_router_id配置不一致
3、硬件故障：心跳线坏了、网卡坏了、IP配置冲突等</code></pre></div>
<h3 id="配置keepalived">配置keepalived</h3>

<h4 id="1-主机-keepalived-conf配置">1. 主机 keepalived.conf配置</h4>

<p>执行 <code>vi /etc/keepalived/keepalived.conf</code> 修改keepalived配置。</p>

<p>修改keepalived配置以后要执行 <code>systemctl restart keepalived</code> 命令重启 keepalived服务</p>

<blockquote>
<p>主机 10.155.10.171</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">! Configuration File for keepalived

global_defs {
    #全局routerid，可以设置为ip
    router_id 10.155.10.171
    vrrp_skip_check_adv_addr
    vrrp_garp_interval 0
    vrrp_gna_interval 0
    #如果需要调用脚本，需要配置执行脚本的用户
    script_user root
    #配置允许执行脚本
    enable_script_security
}

#虚拟实例
vrrp_instance JENKINS2_TRUNK {
    #主机设置为master
    state MASTER
    #选取ifconfig -a 后除了docker和lo外的第一个物理网卡配置
    interface em1
    # vrrp_instance的id，主备必须配置相同
    virtual_router_id 188
    priority 100
    advert_int 1
    authentication {
        # PASS 为密码类型
        auth_type PASS
        # 密码 主备keepalive配置要一致
        auth_pass StupidMan
    }
    virtual_ipaddress {
        # 虚拟ip地址
        10.155.10.167
    }
}
#虚拟服务， ip设置与  vrrp_instance的 virtual_ipaddress 一致，端口为应用服务的端口，如trunk jenkins服务端口8093
virtual_server 10.155.10.167 8093 {
    delay_loop 6
    lb_algo rr 
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    #真实服务器，有几台机器就配置几个，主备都要在这里包括，ip为真实服务的物理ip和应用的端口，本trunk配置的端口为8093
    real_server 10.155.10.171 8093 {
        weight 3
        #服务停掉时调用的脚本
        notify_down &#34;/tol/data/jenkins-scripts/jenkins2-trunk/script_notify.sh down&#34;
        #服务启动时调用的脚本
        notify_up &#34;/tol/data/jenkins-scripts/jenkins2-trunk/script_notify.sh up&#34;
        #对配置的此真实器的对应服务进行健康检查
        HTTP_GET {
            url {
                path /login
                status_code 200
            }
            connect_timeout 3
            //
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 10.155.10.174 8093 {
        weight 3
        HTTP_GET {
            url {
                path /login
                status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

}</code></pre></div>
<blockquote>
<p>10.155.10.172</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">    ! Configuration File for keepalived
    
    global_defs {
       router_id 10.155.10.172
       vrrp_skip_check_adv_addr
       vrrp_garp_interval 0
       vrrp_gna_interval 0
       script_user root
       enable_script_security
    }
    
    vrrp_instance JENKINS2_NEIBU {
        state MASTER
        interface em1
        virtual_router_id 185
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass StupidMan
        }
        virtual_ipaddress {
            10.155.10.185
        }
    }
    
    virtual_server 10.155.10.185 8091 {
        delay_loop 6
        lb_algo rr 
        lb_kind NAT
        persistence_timeout 50
        protocol TCP
    
        #sorry_server 192.168.200.200 1358
    
        real_server 10.155.10.172 8091 {
    	weight 3
    	notify_down &#34;/tol/data/jenkins-scripts/jenkins2-neibu/script_notify.sh down&#34;
    	notify_up &#34;/tol/data/jenkins-scripts/jenkins2-neibu/script_notify.sh up&#34;
            HTTP_GET {
                url { 
                  path /login
                  status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    
        real_server 10.155.10.174 8091 {
            weight 1
            HTTP_GET {
                url {
                  path /login
                  status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        } 
    }</code></pre></div>
<blockquote>
<p>10.155.10.173</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">    ! Configuration File for keepalived
    
    global_defs {
       router_id 10.155.10.173
       vrrp_skip_check_adv_addr
       vrrp_garp_interval 0
       vrrp_gna_interval 0
       script_user root
       enable_script_security
    }
    
    vrrp_instance JENKINS2_RELEASE {
        state MASTER
        interface em1
        virtual_router_id 186
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass StupidMan
        }
        virtual_ipaddress {
            10.155.10.186
        }
    }
    
    virtual_server 10.155.10.186 8092 {
        delay_loop 6
        lb_algo rr 
        lb_kind NAT
        persistence_timeout 50
        protocol TCP
    
        real_server 10.155.10.173 8092 {
    	weight 3
    	notify_down &#34;/tol/data/jenkins-scripts/jenkins2-release/script_notify.sh down&#34;
    	notify_up &#34;/tol/data/jenkins-scripts/jenkins2-release/script_notify.sh up&#34;
            HTTP_GET {
                url { 
                  path /login
                  status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    
        real_server 10.155.10.174 8092 {
            weight 1
            HTTP_GET {
                url {
                  path /login
                  status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        } 
    }</code></pre></div>
<h4 id="2-备机-keepalived-conf配置">2. 备机 keepalived.conf配置</h4>

<p>修改keepalived配置以后要执行 <code>systemctl restart keepalived</code> 命令重启 keepalived服务</p>

<blockquote>
<p>10.155.10.174</p>
</blockquote>

<p>备机的keepalived.conf配置大部分都与主机的相同
因为171 trunk ，172 neibu ，173 release 的备机都在 174上进行的配置，所以这个的keepalived.conf配置中有多个 <code>vrrp_instance</code></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">    ! Configuration File for keepalived
    
    global_defs {
       router_id 10.155.10.174
       vrrp_skip_check_adv_addr
       vrrp_garp_interval 0
       vrrp_gna_interval 0
       script_user root
       enable_script_security
    }
    
    vrrp_instance JENKINS2_TRUNK {
        #此处不同，默认给 BACKUP,主机down机或恢复时，会根据VIP争夺自动变为MASTER或BACKUP
        state BACKUP
        interface em1
        #此处的id必须与主机的virtual_router_id 一致，否则可能会导致虚拟ip不能访问的问题
        virtual_router_id 167
        #权重要配置的比主机小，主机配置的是100，这里配置了50
        priority 50
        advert_int 1
        #此处必须与主机一致，否则会出连通的问题
        authentication {
            auth_type PASS
            auth_pass StupidMan
        }
        virtual_ipaddress {
            10.155.10.167
        }
        #当该虚拟实例状态变为MASTER时要调用的脚本
        notify_master &#34;/tol/app/jenkins2-trunk/script_notify.sh master&#34;
        #当该虚拟实例状态变为BACKUP时要调用的脚本
        notify_backup &#34;/tol/app/jenkins2-trunk/script_notify.sh backup&#34;
    }
    
    virtual_server 10.155.10.167 8093 {
        delay_loop 6
        lb_algo rr 
        lb_kind NAT
        persistence_timeout 50
        protocol TCP
    
        real_server 10.155.10.171 8093 {
    	    weight 3
            HTTP_GET {
                url { 
                  path /login
                  status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    
        real_server 10.155.10.174 8093 {
            weight 3
            HTTP_GET {
                url {
                  path /login
                  status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    
    }
    
    vrrp_instance JENKINS2_NEIBU {
        state BACKUP
        interface em1
        virtual_router_id 185
        priority 50
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass StupidMan
        }
        virtual_ipaddress {
            10.155.10.185
        }
        notify_master &#34;/tol/app/jenkins2-neibu/script_notify.sh master&#34;
        notify_backup &#34;/tol/app/jenkins2-neibu/script_notify.sh backup&#34;
    }
    
    virtual_server 10.155.10.185 8091 {
        delay_loop 6
        lb_algo rr 
        lb_kind NAT
        persistence_timeout 50
        protocol TCP
        
        #sorry_server 192.168.200.200 1358
        
        real_server 10.155.10.172 8091 {
            weight 3
            HTTP_GET { 
                url {  
                  path /login
                  status_code 200
                } 
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }   
        }   
        
        real_server 10.155.10.174 8091 {
            weight 1
            HTTP_GET {
                url {
                  path /login
                  status_code 200
                } 
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }   
        }   
        
    }
    vrrp_instance JENKINS2_RELEASE {
        state BACKUP
        interface em1
        virtual_router_id 186
        priority 50
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass StupidMan
        }
        virtual_ipaddress {
            10.155.10.186
        }
        notify_master &#34;/tol/app/jenkins2-release/script_notify.sh master&#34;
        notify_backup &#34;/tol/app/jenkins2-release/script_notify.sh backup&#34;
    }
    
    virtual_server 10.155.10.186 8092 {
        delay_loop 6
        lb_algo rr 
        lb_kind NAT
        persistence_timeout 50
        protocol TCP
        
        real_server 10.155.10.173 8092 {
            weight 3
            HTTP_GET { 
                url {  
                  path /login
                  status_code 200
                } 
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }   
        }   
        
        real_server 10.155.10.174 8092 {
            weight 1
            HTTP_GET {
                url {
                  path /login
                  status_code 200
                } 
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }   
        }   
    }</code></pre></div>
<h4 id="3-检查keepalived状态">3. 检查keepalived状态</h4>

<p>状态为<code>running</code> 说明正常启动</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#ce5c00;font-weight:bold">[</span>root@cygs-09-10.171-jiaogou.cn ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># systemctl status keepalived</span>
● keepalived.service - LVS and VRRP High Availability Monitor
    Loaded: loaded <span style="color:#ce5c00;font-weight:bold">(</span>/usr/lib/systemd/system/keepalived.service<span style="color:#000;font-weight:bold">;</span> enabled<span style="color:#000;font-weight:bold">;</span> vendor preset: disabled<span style="color:#ce5c00;font-weight:bold">)</span>
    Active: active <span style="color:#ce5c00;font-weight:bold">(</span>running<span style="color:#ce5c00;font-weight:bold">)</span> since 四 2020-02-20 01:19:45 CST<span style="color:#000;font-weight:bold">;</span> 17h ago
    Process: <span style="color:#0000cf;font-weight:bold">47502</span> <span style="color:#000">ExecStart</span><span style="color:#ce5c00;font-weight:bold">=</span>/usr/sbin/keepalived <span style="color:#000">$KEEPALIVED_OPTIONS</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">code</span><span style="color:#ce5c00;font-weight:bold">=</span>exited, <span style="color:#000">status</span><span style="color:#ce5c00;font-weight:bold">=</span>0/SUCCESS<span style="color:#ce5c00;font-weight:bold">)</span>
    Main PID: <span style="color:#0000cf;font-weight:bold">47503</span> <span style="color:#ce5c00;font-weight:bold">(</span>keepalived<span style="color:#ce5c00;font-weight:bold">)</span>
    Tasks: <span style="color:#0000cf;font-weight:bold">3</span>
    Memory: 2.1M
    CGroup: /system.slice/keepalived.service
            ├─47503 /usr/sbin/keepalived -D
            ├─47504 /usr/sbin/keepalived -D
            └─47505 /usr/sbin/keepalived -D</code></pre></div>
<h4 id="4-配置keepalived调用的脚本">4. 配置keepalived调用的脚本</h4>

<blockquote>
<p><strong>10.55.10.171 主机script_notify.sh脚本</strong>，172，173除了ip和目录以外，其余基本一致</p>
</blockquote>

<p>位置 <code>/tol/data/jenkins-scripts/jenkins2-trunk/script-notify.sh</code></p>

<p>主机的<code>script_notify.sh</code>脚本的主要作用:</p>

<p>1.当主机的keepalived检测到jenkins应用的状态可用或不可用时，提供调用<code>notify_down</code> 时使用的脚本</p>

<p>2.提供备节点 <code>state</code>变为<code>MASTER</code> 时，远程调用主机脚本用于杀掉主节点的 jenkins应用进程 ，启动jenkins进程，启动keepalived服务</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#8f5902;font-style:italic">#!/bin/bash
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">case</span> <span style="color:#000">$1</span> in
down<span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S  jenkins service down&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins-trunk/logs/jenkins-notify.log
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S  stop keepalived&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins-trunk/logs/jenkins-notify.log
<span style="color:#8f5902;font-style:italic">#停掉keepalived服务，如果应用服务down掉但不停掉keepalived服务的话，VIP不会转移到备用服务器的服务器上</span>
systemctl stop keepalived
<span style="color:#8f5902;font-style:italic">#如果不加这句 在VIP切换以后，虚拟IP可能会无法访问</span>
ipvsadm -C

<span style="color:#204a87">exit</span> <span style="color:#0000cf;font-weight:bold">1</span>
<span style="color:#000;font-weight:bold">;</span><span style="color:#000;font-weight:bold">;</span>
up<span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S this notify_script is called [up]&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins-trunk/logs/jenkins-notify.log
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S run /tol/app/jenkins-trunk/bin/shutdown.sh and sleep 10s&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins-trunk/logs/jenkins-notify.log
<span style="color:#8f5902;font-style:italic">#关闭jenkins服务</span>
sh /tol/app/jenkins-trunk/bin/shutdown.sh
sleep 10s
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S run /tol/app/jenkins-trunk/bin/startup.sh and sleep 45s&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins-trunk/logs/jenkins-notify.log
<span style="color:#8f5902;font-style:italic">#启动jenkins服务</span>
sh /tol/app/jenkins-trunk/bin/startup.sh
sleep 45s
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S start keepalived&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins-trunk/logs/jenkins-notify.log
<span style="color:#8f5902;font-style:italic">#启动keepalived，启动以后会自动进行应用状态检查，如果应用仍没有正常启动，会继续走notify_down逻辑</span>
systemctl start keepalived
<span style="color:#8f5902;font-style:italic">#清空虚拟服务ipvs规则，有时候虚拟ip访问不了，执行这个命令就可以</span>
ipvsadm -C
<span style="color:#204a87">exit</span> <span style="color:#0000cf;font-weight:bold">1</span>
<span style="color:#000;font-weight:bold">;</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">esac</span></code></pre></div>
<blockquote>
<p><strong>10.55.10.174 备机 script_notify.sh</strong></p>
</blockquote>

<p>备机脚本在 <code>/tol/app/jenkins2-{env}/ 目录中</code></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#8f5902;font-style:italic">#!/bin/bash
</span><span style="color:#8f5902;font-style:italic"></span>#
<span style="color:#204a87;font-weight:bold">case</span> <span style="color:#000">$1</span> in
backup<span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S i became backup&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins2-trunk/jenkins-notify.log
<span style="color:#204a87">exit</span> <span style="color:#0000cf;font-weight:bold">1</span>
<span style="color:#000;font-weight:bold">;</span><span style="color:#000;font-weight:bold">;</span>
master<span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S i became master&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins2-trunk/jenkins-notify.log
ipvsadm -C
ssh 10.155.10.171 <span style="color:#4e9a06">&#34;sh /tol/data/jenkins-scripts/jenkins2-trunk/script_notify.sh up&#34;</span>
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S called remote script&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins2-trunk/jenkins-notify.log
<span style="color:#204a87">exit</span> <span style="color:#0000cf;font-weight:bold">1</span>
<span style="color:#000;font-weight:bold">;</span><span style="color:#000;font-weight:bold">;</span>
down<span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S  jenkins service down&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins2-trunk/jenkins-notify.log
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S  stop keepalived&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins2-trunk/jenkins-notify.log
systemctl stop keepalived
ipvsadm -C

<span style="color:#204a87">exit</span> <span style="color:#0000cf;font-weight:bold">1</span>
<span style="color:#000;font-weight:bold">;</span><span style="color:#000;font-weight:bold">;</span>
up<span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#204a87">echo</span> <span style="color:#4e9a06">`</span>date +<span style="color:#4e9a06">&#34;%Y-%m-%d %H:%M:%S this script is called&#34;</span><span style="color:#4e9a06">`</span> &gt;&gt; /tol/app/jenkins2-trunk/jenkins-notify.log
<span style="color:#204a87">exit</span> <span style="color:#0000cf;font-weight:bold">1</span>
<span style="color:#000;font-weight:bold">;</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">esac</span></code></pre></div>
<h4 id="5-配置主服务器工作目录向备服务器工作目录同步的定时任务">5. 配置主服务器工作目录向备服务器工作目录同步的定时任务</h4>

<blockquote>
<p>主服务器同步脚本</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#8f5902;font-style:italic">#!/bin/bash
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#清空name.txt，没有就新创建</span>
&gt; name.txt
<span style="color:#8f5902;font-style:italic">#将jobs目录中的modules builds目录生成到name.txt中做为rsync的exclude用</span>
<span style="color:#204a87;font-weight:bold">for</span> line in <span style="color:#4e9a06">`</span>ls /tol/htdocs/jenkins-trunk/.jenkins/jobs/<span style="color:#4e9a06">`</span>
<span style="color:#204a87;font-weight:bold">do</span>
    <span style="color:#204a87">echo</span> <span style="color:#4e9a06">&#34;jobs/&#34;</span><span style="color:#000">$line</span><span style="color:#4e9a06">&#34;/modules/&#34;</span> &gt;&gt; name.txt
    <span style="color:#204a87">echo</span> <span style="color:#4e9a06">&#34;jobs/&#34;</span><span style="color:#000">$line</span><span style="color:#4e9a06">&#34;/builds/&#34;</span> &gt;&gt; name.txt
<span style="color:#204a87;font-weight:bold">done</span>
<span style="color:#8f5902;font-style:italic">#将jenkins工作空间的文件（除了modules和builds）都同步到备用服务器对应服务的工作空间中</span>
rsync -avz --exclude-from<span style="color:#ce5c00;font-weight:bold">=</span>/tol/data/jenkins-scripts/jenkins2-trunk/name.txt  --exclude<span style="color:#ce5c00;font-weight:bold">=</span>workspace/ /tol/htdocs/jenkins-trunk/.jenkins/ 10.155.10.174:/tol/data/jenkins-trunk
<span style="color:#8f5902;font-style:italic">#调用备用服务器脚本重启备用服务器的jenkins服务</span>
ssh 10.155.10.174 <span style="color:#4e9a06">&#34;sh /tol/app/jenkins2-trunk/restart.sh&#34;</span></code></pre></div>
<blockquote>
<p>备服务器重启脚本</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#8f5902;font-style:italic">#!/bin/bash
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">process_id</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#204a87;font-weight:bold">$(</span>ps -ef<span style="color:#000;font-weight:bold">|</span>grep 8093<span style="color:#000;font-weight:bold">|</span>grep -v grep<span style="color:#000;font-weight:bold">|</span>awk <span style="color:#4e9a06">&#39;{print $2}&#39;</span><span style="color:#204a87;font-weight:bold">)</span>
<span style="color:#204a87;font-weight:bold">if</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">[</span> ! -z <span style="color:#4e9a06">&#34;</span><span style="color:#000">$process_id</span><span style="color:#4e9a06">&#34;</span> <span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#204a87;font-weight:bold">then</span>
    <span style="color:#204a87">echo</span> <span style="color:#000">$process_id</span> not empty
    <span style="color:#204a87">echo</span> <span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">killed process </span><span style="color:#000">$process_id</span><span style="color:#4e9a06">&#34;</span>
    <span style="color:#204a87">kill</span> -9 <span style="color:#000">$process_id</span>
    <span style="color:#204a87">echo</span> <span style="color:#4e9a06">&#34;start jenkins&#34;</span>
    <span style="color:#204a87">source</span> /etc/profile
    <span style="color:#000">JENKINS_HOME</span><span style="color:#ce5c00;font-weight:bold">=</span>/tol/data/jenkins-trunk
    <span style="color:#204a87">export</span> JENKINS_HOME
    <span style="color:#204a87">echo</span> <span style="color:#000">$JAVA_HOME</span> &gt;&gt; /tol/app/jenkins2-trunk/jenkins-notify.log
    <span style="color:#000">JAVA_CMD</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">nohup </span><span style="color:#000">$JAVA_HOME</span><span style="color:#4e9a06">/bin/java -jar /tol/app/jenkins2-trunk/jenkins-2.204.2.war --httpPort=8093 &gt; /tol/app/jenkins2-trunk/jenkins.log 2&gt;&amp;1 &amp;</span><span style="color:#4e9a06">&#34;</span>  
    <span style="color:#204a87">eval</span> <span style="color:#000">$JAVA_CMD</span>
    <span style="color:#204a87">echo</span> <span style="color:#4e9a06">&#34;jenkins started pid &#34;</span><span style="color:#204a87;font-weight:bold">$(</span>ps -ef<span style="color:#000;font-weight:bold">|</span>grep 8093<span style="color:#000;font-weight:bold">|</span>grep -v grep<span style="color:#000;font-weight:bold">|</span>awk <span style="color:#4e9a06">&#39;{print $2}&#39;</span><span style="color:#204a87;font-weight:bold">)</span>
<span style="color:#204a87;font-weight:bold">else</span>
    <span style="color:#204a87">echo</span> empty
    <span style="color:#204a87">source</span> /etc/profile
    <span style="color:#000">JENKINS_HOME</span><span style="color:#ce5c00;font-weight:bold">=</span>/tol/data/jenkins-trunk
    <span style="color:#204a87">export</span> JENKINS_HOME
    <span style="color:#204a87">echo</span> <span style="color:#000">$JAVA_HOME</span> &gt;&gt; /tol/app/jenkins2-trunk/jenkins-notify.log
    <span style="color:#000">JAVA_CMD</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">nohup </span><span style="color:#000">$JAVA_HOME</span><span style="color:#4e9a06">/bin/java -jar /tol/app/jenkins2-trunk/jenkins-2.204.2.war --httpPort=8093 &gt; /tol/app/jenkins2-trunk/jenkins.log 2&gt;&amp;1 &amp;</span><span style="color:#4e9a06">&#34;</span>
    <span style="color:#204a87">eval</span> <span style="color:#000">$JAVA_CMD</span>
    <span style="color:#204a87">echo</span> <span style="color:#4e9a06">&#34;jenkins started pid &#34;</span><span style="color:#204a87;font-weight:bold">$(</span>ps -ef<span style="color:#000;font-weight:bold">|</span>grep 8093<span style="color:#000;font-weight:bold">|</span>grep -v grep<span style="color:#000;font-weight:bold">|</span>awk <span style="color:#4e9a06">&#39;{print $2}&#39;</span><span style="color:#204a87;font-weight:bold">)</span>
<span style="color:#204a87;font-weight:bold">fi</span></code></pre></div>
<blockquote>
<p>定时任务脚本配置</p>
</blockquote>

<p>各个环境仅脚本目录不同，其他都一致</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">    <span style="color:#0000cf;font-weight:bold">0</span> * * * * sh /tol/data/jenkins-scripts/jenkins2-trunk/rsyncto174.sh <span style="color:#000;font-weight:bold">&amp;</span>&gt;/dev/null</code></pre></div>
<h4 id="问题">问题</h4>

<blockquote>
<p>keepalive是否支持单机使用?</p>
</blockquote>

<p>没试，感觉不行</p>

<ol>
<li>因为目前的jenkins配置是 master+nodes的形式，</li>
<li>因为虚拟ip配置的是不同的realserver 相同的端口，所以没有尝试 一个虚拟ip，配置两个相同ip但端口不同的 realserver，不知道配置了以后虚拟ip使用相同的端口是否能正常的访问到两个服务，而且keepalived进行VIP争夺也有可能有问题</li>
</ol>

<blockquote>
<p>virtual_server的作用是什么？</p>
</blockquote>

<p>提供虚拟ip对外暴露，主节点down机VIP自动漂移到备用节点的服务器</p>

<blockquote>
<p>notify脚本未执行问题</p>
</blockquote>

<p>1、检查 <code>global_defs</code> 中是否增加如两句</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">    script_user root   #指定脚本执行为root
    enable_script_security #允许执行脚本</code></pre></div>
<p>2、如果要输出日志，日志文件位置必须为绝对路径，否则可能输出到别的目录中，误以为脚本未执行</p>

<blockquote>
<p>有的文档上写着要执行以下语句，经测试，不执行也没有问题，keepalived会自动操作</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">    <span style="color:#8f5902;font-style:italic"># 这个命令master机上不用执行 备机上不执行也没事，有keepalived了</span>
    ipvsadm -A -t 10.155.10.188:8080 -s wrr
    <span style="color:#8f5902;font-style:italic">#-A添加虚拟服务器 -t tcp -s指定负载调度算法</span>
    ipvsadm -a -t 10.155.10.188:8080 -r 10.155.10.171:8080 -m
    ipvsadm -a -t 10.155.10.188:8080 -r 10.155.10.172:8080 -m
    <span style="color:#8f5902;font-style:italic">#执行以上命令出现 no address or device的问题的时候，先使用 ipvsadm -ln 看是否已存在对应的设置</span> 
    <span style="color:#8f5902;font-style:italic">#删除配置</span>
    <span style="color:#ce5c00;font-weight:bold">[</span>root@lb01 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># ipvsadm -D -t 10.188.188.188:80</span>
    <span style="color:#ce5c00;font-weight:bold">[</span>root@lb01 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># ipvsadm -d -t 10.188.188.188:80 -r 10.155.10.171:8080</span>
    <span style="color:#ce5c00;font-weight:bold">[</span>root@lb01 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># ipvsadm -d -t 10.188.188.188:80 -r 10.155.10.172:8081</span></code></pre></div>
<blockquote>
<p>如果虚拟ip不能正常访问</p>
</blockquote>

<ol>
<li>检查keepalived配置是否正确，如主备的配置文件的router_id, pass，虚拟ip等配置是否一致</li>
<li>使用命令 <code>systemctl status keepalived</code> 查看keepalived的状态，如果有错误提示信息，也会在里面输出</li>
<li>如果配置都正确，并且服务正常启动没有错误信息，则在主服务器上执行 <code>ipvsadm -C</code> 后再访问虚拟ip是否可以正常访问。（此问题复现很多次，所以我在notify_down, notify_master等脚本中添加都添加<code>ipvsadm -C</code> 命令的执行）</li>
</ol>

<blockquote>
<p><strong>LVS术语</strong></p>

<p>Director Server：调度服务器</p>
</blockquote>

<p>将负载分发到real server的服务器，<strong>==我这里直接将调度服务器和real_server的主节点配置到一起了==</strong></p>

<blockquote>
<p>Real Server：真实服务器</p>
</blockquote>

<p>真正提供应用服务的服务器</p>

<blockquote>
<p>VIP：虚拟IP</p>
</blockquote>

<p>公布给用户访问的虚拟IP地址</p>

<blockquote>
<p>RIP：真实IP</p>
</blockquote>

<p>集群节点上使用的IP地址</p>

<blockquote>
<p>DIP：调度器连接服务器的ip地址</p>
</blockquote>

<h3 id="详细keepalived参数说明">详细keepalived参数说明</h3>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">! Configuration File for keepalived     #全局定义
    
global_defs {
    notification_email {  
        #指定keepalived在发生事件时(比如切换)发送通知邮件的邮箱
        ops@wangshibo.cn   #设置报警邮件地址，可以设置多个，每行一个。 需开启本机的sendmail服务
        tech@wangshibo.cn
    }
        
    notification_email_from ops@wangshibo.cn   #keepalived在发生诸如切换操作时需要发送email通知地址
    smtp_server 127.0.0.1      #指定发送email的smtp服务器
    smtp_connect_timeout 30    #设置连接smtp server的超时时间
    router_id master-node      #运行keepalived的机器的一个标识，通常可设为hostname。故障发生时，发邮件时显示在邮件主题中的信息。
}
    
vrrp_script chk_http_port {      #检测nginx服务是否在运行。有很多方式，比如进程，用脚本检测等等
    script &#34;/opt/chk_nginx.sh&#34;   #这里通过脚本监测
    interval 2                   #脚本执行间隔，每2s检测一次
    weight -5                    #脚本结果导致的优先级变更，检测失败（脚本返回非0）则优先级 -5
    fall 2                    #检测连续2次失败才算确定是真失败。会用weight减少优先级（1-255之间）
    rise 1                    #检测1次成功就算成功。但不修改优先级
}
    
vrrp_instance VI_1 {    #keepalived在同一virtual_router_id中priority（0-255）最大的会成为master，也就是接管VIP，当priority最大的主机发生故障后次priority将会接管
    state MASTER        #指定keepalived的角色，MASTER表示此主机是主服务器，BACKUP表示此主机是备用服务器。注意这里的state指定instance(Initial)的初始状态，就是说在配置好后，这台服务器的初始状态就是这里指定的，但这里指定的不算，还是得要通过竞选通过优先级来确定。如果这里设置为MASTER，但如若他的优先级不及另外一台，那么这台在发送通告时，会发送自己的优先级，另外一台发现优先级不如自己的高，那么他会就回抢占为MASTER
    interface em1          #指定HA监测网络的接口。实例绑定的网卡，因为在配置虚拟IP的时候必须是在已有的网卡上添加的
    mcast_src_ip 103.110.98.14  # 发送多播数据包时的源IP地址，这里注意了，这里实际上就是在哪个地址上发送VRRP通告，这个非常重要，一定要选择稳定的网卡端口来发送，这里相当于heartbeat的心跳端口，如果没有设置那么就用默认的绑定的网卡的IP，也就是interface指定的IP地址
    virtual_router_id 51         #虚拟路由标识，这个标识是一个数字，同一个vrrp实例使用唯一的标识。即同一vrrp_instance下，MASTER和BACKUP必须是一致的
    priority 101                 #定义优先级，数字越大，优先级越高，在同一个vrrp_instance下，MASTER的优先级必须大于BACKUP的优先级
    advert_int 1                 #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔，单位是秒
    authentication {             #设置验证类型和密码。主从必须一样
        auth_type PASS           #设置vrrp验证类型，主要有PASS和AH两种
        auth_pass 1111           #设置vrrp验证密码，在同一个vrrp_instance下，MASTER与BACKUP必须使用相同的密码才能正常通信
    }
    virtual_ipaddress {          #VRRP HA 虚拟地址 如果有多个VIP，继续换行填写
        103.110.98.20
    }
    
    track_script {                      #执行监控的服务。注意这个设置不能紧挨着写在vrrp_script配置块的后面（实验中碰过的坑），否则nginx监控失效！！
        chk_http_port                    #引用VRRP脚本，即在 vrrp_script 部分指定的名字。定期运行它们来改变优先级，并最终引发主备切换。
    }
    notify_master “xxxxx start”     # 当成为master时调用脚本
    notify_backup “xxxxx stop”      # 当成为backup时调用脚本
}</code></pre></div>
<h2 id="参考文档">参考文档</h2>

<h3 id="jenkins备份及高可用">jenkins备份及高可用</h3>

<p>ThinBackUp插件备份配置</p>

<blockquote>
<p><a href="https://cloud.tencent.com/developer/article/1487934">https://cloud.tencent.com/developer/article/1487934</a></p>
</blockquote>

<p>ThinBackUp插件备份配置说明</p>

<blockquote>
<p><a href="https://blog.51cto.com/lzlutao/2378440">https://blog.51cto.com/lzlutao/2378440</a></p>
</blockquote>

<p>主子节点安装配置</p>

<blockquote>
<p><a href="https://www.howtoforge.com/tutorial/ubuntu-jenkins-master-slave/">https://www.howtoforge.com/tutorial/ubuntu-jenkins-master-slave/</a></p>

<p><a href="https://cloud.tencent.com/developer/article/1485312">https://cloud.tencent.com/developer/article/1485312</a></p>

<p><a href="https://www.sohu.com/a/157896231_825425">https://www.sohu.com/a/157896231_825425</a></p>
</blockquote>

<p>主子备份</p>

<blockquote>
<p><a href="https://www.cnblogs.com/shengulong/p/6764452.html">https://www.cnblogs.com/shengulong/p/6764452.html</a></p>
</blockquote>

<p>jenkins2高可用方案</p>

<blockquote>
<p><a href="https://www.jianshu.com/p/223bedd1097d">https://www.jianshu.com/p/223bedd1097d</a></p>

<p><a href="https://wangdj104.github.io/2019/07/15/jenkins%E9%AB%98%E5%8F%AF%E7%94%A8%E9%83%A8%E7%BD%B2%E6%96%87%E6%A1%A3/">https://wangdj104.github.io/2019/07/15/jenkins%E9%AB%98%E5%8F%AF%E7%94%A8%E9%83%A8%E7%BD%B2%E6%96%87%E6%A1%A3/</a></p>
</blockquote>

<h3 id="keepalived参考文档">keepalived参考文档</h3>

<p>卸载keepalived</p>

<blockquote>
<p><a href="https://blog.csdn.net/lyl_zsu/article/details/84915422">https://blog.csdn.net/lyl_zsu/article/details/84915422</a></p>
</blockquote>

<p>lvs 安装 + keepalive配置</p>

<blockquote>
<p><a href="https://blog.51cto.com/zaizheni/2349355">https://blog.51cto.com/zaizheni/2349355</a></p>
</blockquote>

<p>这篇讲的是keepalive + mysql负载的，对keepalive讲的很细</p>

<blockquote>
<p><a href="https://www.jianshu.com/p/3a80a20166c0">https://www.jianshu.com/p/3a80a20166c0</a></p>
</blockquote>

<p>虚拟ip无法访问的问题解决</p>

<blockquote>
<p><a href="https://blog.csdn.net/luckyzsion/article/details/87272920">https://blog.csdn.net/luckyzsion/article/details/87272920</a></p>
</blockquote>

<p>安装配置启动，比较详细</p>

<blockquote>
<p><a href="http://www.studyshare.cn/software/details/1158/0">http://www.studyshare.cn/software/details/1158/0</a></p>
</blockquote>

<p>配置参数说明</p>

<blockquote>
<p><a href="https://blog.csdn.net/qq_26545305/article/details/79957992">https://blog.csdn.net/qq_26545305/article/details/79957992</a></p>
</blockquote>

<p>安装比较详细，配置有些模糊</p>

<blockquote>
<p><a href="https://blog.csdn.net/bbwangj/article/details/80346428">https://blog.csdn.net/bbwangj/article/details/80346428</a></p>
</blockquote>

<p>高可用配置</p>

<blockquote>
<p><a href="https://wangdj104.github.io/2019/07/15/jenkins%E9%AB%98%E5%8F%AF%E7%94%A8%E9%83%A8%E7%BD%B2%E6%96%87%E6%A1%A3/">https://wangdj104.github.io/2019/07/15/jenkins%E9%AB%98%E5%8F%AF%E7%94%A8%E9%83%A8%E7%BD%B2%E6%96%87%E6%A1%A3/</a></p>
</blockquote>

<p>keepalive官方文档</p>

<blockquote>
<p><a href="https://max.book118.com/html/2018/1120/7016032164001160.shtm">https://max.book118.com/html/2018/1120/7016032164001160.shtm</a></p>
</blockquote>

<p>卸载 keepalive</p>

<blockquote>
<p><a href="https://blog.csdn.net/lyl_zsu/article/details/84915422">https://blog.csdn.net/lyl_zsu/article/details/84915422</a></p>
</blockquote>

<p>keepalive主从架构解析</p>

<blockquote>
<p><a href="https://www.jianshu.com/p/eefa8afabb09">https://www.jianshu.com/p/eefa8afabb09</a></p>
</blockquote>

<p>shell脚本中执行java命令</p>

<blockquote>
<p><a href="https://www.cnblogs.com/xiaoyu1994/p/9240240.html">https://www.cnblogs.com/xiaoyu1994/p/9240240.html</a></p>
</blockquote>

<h3 id="lvs-keepalived-对jenkins-高可用流转图">lvs+keepalived 对jenkins 高可用流转图</h3>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220330143055.png" width = 900 /> </div>

<p>部署图</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220330143113.png" width = 400 /> </div>

    </div>
    <footer class="post-footer">
     
 
<div class="post-tags">     
     
    <a href="http://www.shutdown.cn/tags/lvs" rel="tag" title="lvs">#lvs#</a>
    
    <a href="http://www.shutdown.cn/tags/jenkins" rel="tag" title="jenkins">#jenkins#</a>
    
    <a href="http://www.shutdown.cn/tags/keepalive" rel="tag" title="keepalive">#keepalive#</a>
    
    <a href="http://www.shutdown.cn/tags/%e9%ab%98%e5%8f%af%e7%94%a8" rel="tag" title="高可用">#高可用#</a>
    
    <a href="http://www.shutdown.cn/tags/cicd" rel="tag" title="cicd">#cicd#</a>
    
</div>



     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/learn-about-apollo/" rel="next" title="Apollo学习">
        <i class="fa fa-chevron-left"></i> Apollo学习
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/about/" rel="prev" title="关于我">
        关于我 <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

  <ul class="sidebar-nav motion-element">
    <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
      文章目录
    </li>
    <li class="sidebar-nav-overview" data-target="site-overview">
      站点概览
    </li>
  </ul>

    <section class="site-overview sidebar-panel ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
<section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
    <div class="post-toc">
        <div class="post-toc-content"><nav id="TableOfContents">
<ul>
<li>
<ul>
<li><a href="#部署清单">部署清单</a></li>
<li><a href="#部署步骤">部署步骤</a>
<ul>
<li><a href="#安装和启动ipvsadm">安装和启动ipvsadm</a></li>
<li><a href="#安装和启动keepalived">安装和启动keepalived</a></li>
<li><a href="#配置keepalived">配置keepalived</a>
<ul>
<li><a href="#1-主机-keepalived-conf配置">1. 主机 keepalived.conf配置</a></li>
<li><a href="#2-备机-keepalived-conf配置">2. 备机 keepalived.conf配置</a></li>
<li><a href="#3-检查keepalived状态">3. 检查keepalived状态</a></li>
<li><a href="#4-配置keepalived调用的脚本">4. 配置keepalived调用的脚本</a></li>
<li><a href="#5-配置主服务器工作目录向备服务器工作目录同步的定时任务">5. 配置主服务器工作目录向备服务器工作目录同步的定时任务</a></li>
<li><a href="#问题">问题</a></li>
</ul></li>
<li><a href="#详细keepalived参数说明">详细keepalived参数说明</a></li>
</ul></li>
<li><a href="#参考文档">参考文档</a>
<ul>
<li><a href="#jenkins备份及高可用">jenkins备份及高可用</a></li>
<li><a href="#keepalived参考文档">keepalived参考文档</a></li>
<li><a href="#lvs-keepalived-对jenkins-高可用流转图">lvs+keepalived 对jenkins 高可用流转图</a></li>
</ul></li>
</ul></li>
</ul>
</nav></div>
    </div>
</section>

  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>